package com.itgaoshu.hospitalproject.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itgaoshu.hospitalproject.bean.Type;
import com.itgaoshu.hospitalproject.service.TypeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("type")
public class TypeController {
    @Resource
    private TypeService typeService;

    //查询药品类型的方法
    @RequestMapping("findAllType")
    @ResponseBody
    public Object findAllType(String typeName,Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        List<Type> types = typeService.findAllType(typeName);
        PageInfo pageInfo = new PageInfo(types);
        Map<String,Object> tableData = new HashMap<>();
        tableData.put("msg","");
        tableData.put("code",0);
        tableData.put("count",pageInfo.getTotal());
        tableData.put("data",pageInfo.getList());
        return tableData;
    }


    //添加药品分类的方法
    @ResponseBody
    @RequestMapping("addType")
    public Object addType(Type type){
        int count = typeService.count(type);
        if(count==0){
            int result = typeService.addType(type);
            if(result>0){
                return "添加成功";
            }else{
                return "添加失败";
            }
        }else{
            return "这个分类已经存在";
        }
    }


}
